package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.vo.AccountDetailVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BizAccountDetail;
import com.ruoyi.system.service.IBizAccountDetailService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 账户明细（收入/支出）Controller
 * 
 * @author ruoyi
 * @date 2025-07-01
 */
@RestController
@RequestMapping("/accountDetail/detail")
public class BizAccountDetailController extends BaseController
{
    @Autowired
    private IBizAccountDetailService bizAccountDetailService;

    /**
     * 查询账户明细（收入/支出）列表
     */
    @PreAuthorize("@ss.hasPermi('accountDetail:detail:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizAccountDetail bizAccountDetail)
    {
        startPage();
        List<BizAccountDetail> list = bizAccountDetailService.selectBizAccountDetailList(bizAccountDetail);
        return getDataTable(list);
    }

    /**
     * 导出账户明细（收入/支出）列表
     */
    @PreAuthorize("@ss.hasPermi('accountDetail:detail:export')")
    @Log(title = "账户明细（收入/支出）", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizAccountDetail bizAccountDetail)
    {
        List<BizAccountDetail> list = bizAccountDetailService.selectBizAccountDetailList(bizAccountDetail);
        ExcelUtil<BizAccountDetail> util = new ExcelUtil<BizAccountDetail>(BizAccountDetail.class);
        util.exportExcel(response, list, "账户明细（收入/支出）数据");
    }

    /**
     * 获取账户明细（收入/支出）详细信息
     */
    @PreAuthorize("@ss.hasPermi('accountDetail:detail:query')")
    @GetMapping("/income/{riderId}")
    public TableDataInfo getIncomeList(
            @PathVariable Integer riderId,
            @RequestParam(required = false) String updateTime // 接收 updateTime
    ) {
        // 调用 Service，传递 updateTime 筛选条件
        List<BizAccountDetail> list = bizAccountDetailService.selectIncomeByRiderId(riderId);
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('accountDetail:detail:query')")

    @GetMapping("/expense/{riderId}")
    public TableDataInfo getExpenseList(
            @PathVariable Integer riderId,
            @RequestParam(required = false) String updateTime // 接收 updateTime
    ) {
        // 调用 Service，传递 updateTime 筛选条件
        List<BizAccountDetail> list = bizAccountDetailService.selectExpenseByRiderId(riderId);
        return getDataTable(list);
    }

    /**
     * 新增账户明细（收入/支出）
     */
    @PreAuthorize("@ss.hasPermi('accountDetail:detail:add')")
    @Log(title = "账户明细（收入/支出）", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizAccountDetail bizAccountDetail)
    {
        return toAjax(bizAccountDetailService.insertBizAccountDetail(bizAccountDetail));
    }

    /**
     * 修改账户明细（收入/支出）
     */
    @PreAuthorize("@ss.hasPermi('accountDetail:detail:edit')")
    @Log(title = "账户明细（收入/支出）", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizAccountDetail bizAccountDetail)
    {
        return toAjax(bizAccountDetailService.updateBizAccountDetail(bizAccountDetail));
    }

    /**
     * 删除账户明细（收入/支出）
     */
    @PreAuthorize("@ss.hasPermi('accountDetail:detail:remove')")
    @Log(title = "账户明细（收入/支出）", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(bizAccountDetailService.deleteBizAccountDetailByIds(ids));
    }


    /**
     * 获取单条收入明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('accountDetail:detail:query')")
    @GetMapping(value = "/oneIncome/{id}")
    public AjaxResult getIncomeDetail(@PathVariable("id") Integer id) {
        AccountDetailVO accountDetailVO = bizAccountDetailService.selectIncomeById(id);
        return AjaxResult.success(accountDetailVO);
    }

    /**
     * 获取单条支出明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('accountDetail:detail:query')")
    @GetMapping(value = "/oneExpense/{id}")
    public AjaxResult getExpenseDetail(@PathVariable("id") Integer id) {
        AccountDetailVO accountDetailVO = bizAccountDetailService.selectExpenseById(id);
        return AjaxResult.success(accountDetailVO);
    }
}
